#include "msg_pack_codec.h"
#include <string.h>

int encode_msg_pack( CCSHead& cshead, CMsg &msg, void* buf, size_t length )
{
    if (NULL == buf || length <= 0)
    {
        return -1;
    }

    short outlength = 0;
    short encodelength = length;
    int encoderet = 0;
    encoderet = cshead.Encode((char*)buf, encodelength);
    if (encoderet)
    {
        return -2;
    }

    outlength += encodelength;
    encodelength = length - outlength;
    encoderet = msg.Encode((char*)buf, encodelength);
    if (encoderet)
    {
        return -3;
    }

    outlength += encodelength;
    return outlength;
}

int decode_csmsg_pack( CCSHead *cshead, CMsg *msg, const void* buf, size_t length )
{
    if (NULL == msg || NULL == cshead || NULL == buf || length <= 0)
    {
        return -1;
    }

    int decodelen = 0;
    int decoderet = cshead->Decode((const char*)buf, length);
    if (decoderet < 0)
    {
        return -2;
    }

    decodelen += decoderet;
    decoderet = msg->Decode((const char*)buf + decodelen, length - decodelen);
    if (decoderet < 0)
    {
        return -3;
    }

    decodelen += decoderet;
    return decodelen;
}
